package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.dto.*;
import com.eastfair.venuebooking.entity.FollowRecord;
import com.eastfair.venuebooking.service.FollowRecordService;
import com.eastfair.venuebooking.vo.FollowRecordTimelineVO;
import com.eastfair.venuebooking.vo.FollowRecordVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 跟进记录表
 * </p>
 *
 * @author clm
 * @date 2022-06-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/followRecord")
@Api(value = "FollowRecord", tags = "跟进记录表")
@PreAuth(replace = "venuebooking:followRecord:")
public class FollowRecordController extends SuperController<FollowRecordService, Long, FollowRecord, FollowRecordPageQuery, FollowRecordSaveDTO, FollowRecordUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private FollowRecordService followRecordService;

    @Override
    public void handlerResult(IPage<FollowRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<FollowRecord> followRecordList = list.stream().map((map) -> {
            FollowRecord followRecord = FollowRecord.builder().build();
            //TODO 请在这里完成转换
            return followRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(followRecordList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<FollowRecordPageQuery> params) {
        FollowRecordPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 新增跟进记录.
     *
     * @param saveDTO the follow record
     * @return the r
     */
    @SysLog("新增跟进记录")
    @PostMapping("/save")
    public R<FollowRecord> saveFollowRecord(@RequestBody @Validated({FollowRecordPlanSaveDTO.Save.class})
                                                    FollowRecordPlanSaveDTO saveDTO) {
        log.info("saveFollowRecord - 新增跟进记录, saveDTO={}", saveDTO);

        followRecordService.saveFollowRecordPlan(saveDTO);
        return R.successDef();
    }

    /**
     * 查询跟进记录时间轴.
     *
     * @param query the next start time
     * @return the r
     */
    @PostMapping("/timeline/query")
    public R<List<FollowRecordTimelineVO>> queryTimeLine(@RequestBody FollowRecordTimelineQuery query) {

        R<List<FollowRecordTimelineVO>> r = followRecordService.listFollowRecordTimelineVOs(query);
        return r;
    }


    /**
     * 查询未同步CRM跟进记录
     *
     * @return the r
     */
    @PostMapping("/unsyn/query")
    public R<List<FollowRecordVO>> queryUnsynRecord() {

        List<FollowRecordVO> followRecordVOS = followRecordService.listUnsynRecord();
        return R.success(followRecordVOS);
    }

}
